﻿using System;
using System.Collections;
using System.Collections.Generic;
using DevComponents.AdvTree;
using DevExpress.XtraTreeList.Nodes;

/********************************************************************
 * 作者: Ruiky
 * 创建日期：2013-07-07
 * 功能：TreeView树函数类,主要负责对树节点查找
 ********************************************************************/
namespace NanoERP.Common
{
    public static class TreeViewHelper
    {
        /// <summary>
        /// 树节点查询
        /// </summary>
        /// <param name="tds">表示System.Windows.Forms.TreeNode 对象的集合。</param>
        /// <param name="fun">封装一个具有一个参数并返回 TreeNode 参数指定的类型值的方法</param>
        /// <returns></returns>
        public static IEnumerable<Node> TreeWhere(this NodeCollection tds, Func<Node, bool> fun)
        {
            foreach (Node item in tds)
            {
                if (fun(item)) yield return item;

                var list = item.Nodes.TreeWhere(fun);
                foreach (var item1 in list)
                    yield return item1;
            }
        }

        /// <summary>
        /// 树节点查询
        /// </summary>
        /// <param name="listNodes">>表示System.Windows.Forms.TreeListNodes 对象的集合。</param>
        /// <param name="fun">封装一个具有一个参数并返回 TreeNode 参数指定的类型值的方法</param>
        /// <returns></returns>
        public static IEnumerable<TreeListNode> TreeWhere(this IEnumerable listNodes, Func<TreeListNode, bool> fun)
        {
            foreach (TreeListNode n in listNodes)
            {
                if (fun(n)) yield return n;

                var list = n.Nodes.TreeWhere(fun);
                foreach (var item1 in list)
                    yield return item1;
            }
        }
    }
}
